package com.demo2.todo.model.entity;

import com.demo2.todo.model.enums.ReviewStatus;
import com.demo2.todo.model.enums.TodoStatus;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

/**
 * @author evi
 */
@Data
@Entity
@Table(name = "todos")
@NoArgsConstructor
@AllArgsConstructor
public class Todo {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String content;

    @Column(nullable = false)
    @NotBlank(message = "标题不能为空")
    private String title;

    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private TodoStatus status = TodoStatus.pending;

    @Column(nullable = false)
    @Enumerated(EnumType.STRING)
    private ReviewStatus reviewStatus = ReviewStatus.pending;

    @CreationTimestamp
    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt = LocalDateTime.now();

    // 新增：到期时间
    @Column(name = "deadline")
    private LocalDateTime deadline;

    // 新增：标签关联
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "todo_tags",
            joinColumns = @JoinColumn(name = "todo_id"),
            inverseJoinColumns = @JoinColumn(name = "tag_id")
    )
    private Set<Tag> tags = new HashSet<>();

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;

    // 新增分享相关字段
    @Column(name = "share_token")
    private String shareToken;

    @Column(name = "share_expires_at")
    private LocalDateTime shareExpiresAt;

    // 如需直接操作 userId，可添加（但需避免与 user 字段冲突）
    @Column(name = "user_id", insertable = false, updatable = false) // 不参与 CRUD，仅用于查询
    private Long userId;

    // 举报计数
    private int reportCount = 0;

}

